﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using static HongYeSystemWebApi.Templet.Entity.Model.CommonInfo;

namespace HongYeSystemWebApi.Templet.Core
{
    public class GenerateJwt
    {
        /// <summary>
        /// 生成token
        /// </summary>
        /// <param name="sub"></param>
        /// <param name="customClaims">携带的用户信息</param>
        /// <returns></returns>
        public string GenerateEncodedToken(string sub, JwtUserInfo customClaims)
        {
            //创建用户身份标识，可按需要添加更多信息
            var claims = new List<Claim>
            {
                
                new Claim("UserName", customClaims.UserName),
            
                //new Claim("realname",customClaims.realname),//可以添加一些常用信息
                new Claim(JwtRegisteredClaimNames.Sub, sub),
            };
            //创建令牌
            var jwt = new JwtSecurityToken(
                issuer: CConfigure.Issuer,
                audience: CConfigure.Audience,
                claims: claims,
                notBefore: CConfigure.NotBefore,
                expires: CConfigure.Expiration,
                signingCredentials: CConfigure.SigningCredentials);
            return new JwtSecurityTokenHandler().WriteToken(jwt);
        }
    }
}
